package com.xuan.bkm.core.config.configuration;

import lombok.SneakyThrows;
import org.springframework.core.convert.converter.Converter;
import org.springframework.format.FormatterRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.text.SimpleDateFormat;
import java.util.Date;

public class WebConfig implements WebMvcConfigurer {
    private static final ThreadLocal<SimpleDateFormat> LOCAL=new ThreadLocal<>();

    @Override
    public void addFormatters(FormatterRegistry registry) {
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        if(LOCAL.get()==null){
            LOCAL.set(sdf);
        }
        registry.addConverter(new Converter<String, Date>() {
            @Override
            @SneakyThrows
            public Date convert(String s) {
                Date date = LOCAL.get().parse(s);
                return date;
            }
        });
    }
}
